<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\EbPolicyApplication\logic;

use plugin\saiadmin\basic\BaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
use app\EbPolicyApplication\model\EbPolicyApplication;
use plugin\saiadmin\service\OpenSpoutWriter;

/**
 * 申报列表逻辑层
 */
class EbPolicyApplicationLogic extends BaseLogic
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->model = new EbPolicyApplication();
    }
    
public function export($where = [])
{
    $query = $this->search($where)->with(['PolicyMatching']);
    // ->field('id,name,enterprise_id,sort,status,create_time');
    $data = $this->getAll($query);
   

    $file_name = '政策申报数据.xlsx';
    // 更新表头以匹配新的列数据
    $header = ['申报政策名称', '申报公司名称', '统一社会信用代码', '申报人姓名', '手机号码', '公司职务', '申报时间'];

    
    $filter = [
        // 'type' => [
        //     ['value' => 1, 'label' => '进行中'],
        //     ['value' => 2, 'label' => '已结束'],
        //     ['value' => 3, 'label' => '未开始']
        // ]
    ];
    
    $data_attr = [];
    foreach($data as $value){
        $data_attr[] = [
            'PolicyMatching.policy_title' => $value['PolicyMatching']['policy_title'] ?? '',
            'company_name' => $value['company_name'] ?? '',
            'credit_code' => $value['credit_code'] ?? '',
            'name' => $value['name'] ?? '',
            'phone' => $value['phone'] ?? '',
            'position' => $value['position'] ?? '',
            'create_time' => $value['create_time'] ?? '',
        ];
    }

    $writer = new OpenSpoutWriter($file_name);
    // 更新列宽以匹配新的列数
    $writer->setWidth([20, 20, 20, 20, 20, 20, 20]);


    $writer->setHeader($header);
    $writer->setData($data_attr, null, $filter);
    $file_path = $writer->returnFile();
    return response()->download($file_path, urlencode($file_name));


}

}
